package com.media.themovieworld.util.dialog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.media.themovieworld.R;

public abstract class BaseMovieDialog extends BaseDialogNoTitle{

	private TextView dialogTitle;
	private LinearLayout llSingleButton;
	private LinearLayout llTwoButton;
	private LinearLayout layoutContent;
	private TextView txtSingleButton;
	private TextView txtPositiveButton;
	private TextView txtNegativeButton;
	private OnMovieDialogClickListener mListener;
	
	public BaseMovieDialog(Context context) {
		super(context);
	}

	@Override
	protected int getContentResId() {
		return R.layout.meta__dialog_base_content;
	}

	@Override
	protected void initContentView(LayoutInflater inflater, View contentView) {
		llSingleButton = (LinearLayout) contentView.findViewById(R.id.up_dialog_base_1_button);
		llTwoButton = (LinearLayout) contentView.findViewById(R.id.up_dialog_base_2_buttons);
		layoutContent = (LinearLayout) contentView.findViewById(R.id.up_dialog_base_body);
		dialogTitle = (TextView) contentView.findViewById(R.id.up_dialog_base_title);
		dialogTitle.setText(getDialogTitle());
		txtSingleButton = (TextView) contentView.findViewById(R.id.up_dialog_base_default);
		txtPositiveButton = (TextView) contentView.findViewById(R.id.up_dialog_base_positive);
		txtNegativeButton = (TextView) contentView.findViewById(R.id.up_dialog_base_negative);
		txtSingleButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
				if (mListener!=null) {
					mListener.onSingleButtonClick(txtSingleButton);
				}
			}
		});
		txtNegativeButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
				if (mListener!=null) {
					mListener.onNegativeButtonClick(txtSingleButton);
				}
			}
		});
		txtPositiveButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
				if (mListener!=null) {
					mListener.onPositiveButtonClick(txtSingleButton);
				}
			}
		});
		configDialogButton();
		View v = onCreateContentView(inflater, contentView);
		if (v!=null) {
			layoutContent.addView(v);
		}
	}
	
	public void setSingleButtonText(String text){
		txtSingleButton.setText(text);
	}
	
	public void setPositiveButtonText(String text){
		txtPositiveButton.setText(text);
	}
	
	public void setNegativeButtonText(String text){
		txtNegativeButton.setText(text);
	}
	
	private void configDialogButton(){
		if (isSingleConfirmButton()) {
			llTwoButton.setVisibility(View.GONE);
			llSingleButton.setVisibility(View.VISIBLE);
		}else{
			llSingleButton.setVisibility(View.GONE);
			llTwoButton.setVisibility(View.VISIBLE);
		}
		setTextForButton();
	}
	
	public void setOnMovieDialogClickListener(OnMovieDialogClickListener listener){
		mListener = listener;
	}
	
	public interface OnMovieDialogClickListener{
		public void onSingleButtonClick(View v);
		public void onPositiveButtonClick(View v);
		public void onNegativeButtonClick(View v);
	}
	
	protected abstract String getDialogTitle();
	protected abstract boolean isSingleConfirmButton();
	protected abstract void setTextForButton();
	protected abstract View onCreateContentView(LayoutInflater inflater, View contentView);
}
